package org.glow.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.goods.dao.ParaMapper;
import org.glow.changgou.goods.pojo.Category;
import org.glow.changgou.goods.pojo.Para;
import org.glow.changgou.goods.pojo.Spec;
import org.glow.changgou.goods.service.CategoryService;
import org.glow.changgou.goods.service.ParaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-09-22 20:56:57
 * @Description
 * @Version 1.0
 */
@Service
public class ParaServiceImpl extends ServiceImpl<ParaMapper, Para> implements ParaService {
    @Autowired
    private ParaService paraService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public Page<Para> findPage(Para para, int page, int size) {
        LambdaQueryWrapper<Para> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,para);
        Page<Para> pageInfo = new Page<>(page,size);
        return paraService.page(pageInfo,queryWrapper);
    }

    @Override
    public Page<Para> findPage(int page, int size) {
        Page<Para> pageInfo = new Page<Para>(page,size);
        return paraService.page(pageInfo);
    }

    @Override
    public List<Para> findList(Para para) {
        LambdaQueryWrapper<Para> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,para);
        return paraService.list(queryWrapper);

    }

    @Override
    public void delete(Integer id) {
            paraService.removeById(id);
    }

    @Override
    public void update(Para para) {
            paraService.update(para);
    }

    @Override
    public void add(Para para) {
            paraService.save(para);
    }

    @Override
    public Para findById(Integer id) {
        return paraService.getById(id);
    }

    @Override
    public List<Para> findAll() {
        return paraService.list();
    }

    @Override
    public List<Para> findByCategoryId(Integer id) {
        Category category = categoryService.getById(id);
        LambdaQueryWrapper<Para> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Para::getTemplateId,category.getTemplateId());
        return paraService.list(queryWrapper);
    }

    public LambdaQueryWrapper<Para> getCondition(LambdaQueryWrapper<Para> queryWrapper,Para para){
//        Example example=new Example(Para.class);
//        Example.Criteria criteria = example.createCriteria();
        if(para!=null){
            // id
            if(!StringUtils.isEmpty(para.getId())){
//                criteria.andEqualTo("id",para.getId());
                queryWrapper.eq(Para::getId,para.getId());
            }
            // 名称
            if(!StringUtils.isEmpty(para.getName())){
//                criteria.andLike("name","%"+para.getName()+"%");
                queryWrapper.like(Para::getName,para.getName());
            }
            // 选项
            if(!StringUtils.isEmpty(para.getOptions())){
//                criteria.andEqualTo("options",para.getOptions());
                    queryWrapper.eq(Para::getOptions,para.getOptions());
            }
            // 排序
            if(!StringUtils.isEmpty(para.getSeq())){
//                criteria.andEqualTo("seq",para.getSeq());
                queryWrapper.eq(Para::getSeq,para.getSeq());
            }
            // 模板ID
            if(!StringUtils.isEmpty(para.getTemplateId())){
//                criteria.andEqualTo("templateId",para.getTemplateId());
                    queryWrapper.eq(Para::getTemplateId,para.getTemplateId());
            }
        }
        return queryWrapper;
    }
}
